﻿Public Class frmNhaCungCap

    Private Sub frmNhaCungCap_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Text = "Quản lí nhà cung cấp"
        Dim dt As DataTable = NhaCungCapDAO.LoadNhaCungCap()
        dgvNhaCungCap.DataSource = dt
    End Sub

    Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click
        Dim frm = New frmThem_CapNhatNhaCungCap()
        frm._status = "Them"
        AddHandler frm.FormClosed, AddressOf frmNhaCungCap_FormClosed
        frm.Show()
    End Sub

    Private Sub btnCapNhat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCapNhat.Click
        Dim frm = New frmThem_CapNhatNhaCungCap()
        frm._status = "CapNhat"
        frm._id = Convert.ToInt32(dgvNhaCungCap.Rows(dgvNhaCungCap.CurrentCell.RowIndex).Cells("ID").Value.ToString())
        frm.txtNhaCungCap.Text = dgvNhaCungCap.Rows(dgvNhaCungCap.CurrentCell.RowIndex).Cells("Ten").Value.ToString()
        frm.txtDiaChi.Text = dgvNhaCungCap.Rows(dgvNhaCungCap.CurrentCell.RowIndex).Cells(2).Value.ToString()
        frm.txtSoDienThoai.Text = dgvNhaCungCap.Rows(dgvNhaCungCap.CurrentCell.RowIndex).Cells(3).Value.ToString()
        AddHandler frm.FormClosed, AddressOf frmNhaCungCap_FormClosed
        frm.Show()

    End Sub

    Private Sub btnXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click
        Dim dr = New DialogResult
        dr = MessageBox.Show("Bạn có muốn xóa!!", "", MessageBoxButtons.YesNo, MessageBoxIcon.Warning)
        If DialogResult.Yes = dr Then
            Dim _ncc = New NhaCungCap
            _ncc.m_strTen = dgvNhaCungCap.Rows(dgvNhaCungCap.CurrentCell.RowIndex).Cells("Ten").Value.ToString()
            _ncc.m_strDiaChi = dgvNhaCungCap.Rows(dgvNhaCungCap.CurrentCell.RowIndex).Cells("DiaChi").Value.ToString()
            _ncc.m_strSoDienThoai = dgvNhaCungCap.Rows(dgvNhaCungCap.CurrentCell.RowIndex).Cells("SoDienThoai").Value.ToString()
            NhaCungCapDAO.XoaNhaCungCap(_ncc)
            dgvNhaCungCap.DataSource = NhaCungCapDAO.LoadNhaCungCap()
        Else
            Return
        End If
    End Sub

    Private Sub frmNhaCungCap_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
        dgvNhaCungCap.DataSource = NhaCungCapDAO.LoadNhaCungCap()
    End Sub

    Private Sub Thoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Thoat.Click
        me.Close()
    End Sub

    Private Sub txtTimKiem_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtTimKiem.TextChanged
        If txtTimKiem.Text = "" Then
            dgvNhaCungCap.DataSource = NhaCungCapDAO.LoadNhaCungCap()
        Else
            Dim _ncc = New NhaCungCap
            _ncc.m_strTen = txtTimKiem.Text
            dgvNhaCungCap.DataSource = NhaCungCapDAO.TimKiemNhaCungCap(_ncc)
        End If
    End Sub
End Class